import matplotlib.pyplot as plt

from scipy.interpolate import make_interp_spline
import pandas as pd
import datetime
import numpy as np

plt.rcParams['font.sans-serif'] = ['SimHei']  # 用来正常显示中文标签
name = ['USA', 'Russia', 'India', "Iran", 'China', 'Italy', 'UK', 'Brazil']
name_cn = ['美国', '俄罗斯', '印度', '伊朗', '中国', '意大利', '英国', '巴西']
day = 100


def confirm(name, date, name_cn):
    '''
        当前确诊认识
        :param name: 国家名
        :param date: 时间区间
        :return:
    '''
    value = []
    data = []
    for i in range(0, len(name)):
        OneDayData = pd.read_excel('csv/OneDay/' + name[i] + '_OneDay.xlsx', usecol=['国家', '确诊人数', '更新时间'])
        data.append(OneDayData)
        name.append(OneDayData.values[0][1])
    for i in range(0, len(data)):
        value_temp = []
        for j in range(0, data[i].values.shape[0]):
            value_temp.append(data[i].values[j][2])
        value.append(value_temp)

    for i in range(0, len(value)):
        if (len(value[i]) <= day):
            temp = [0 for i in range(0, day - len(value[i]))]
            value[i] += temp
        else:
            value[i] = value[i][0:day]
        value[i].reverse()

    date_str = []
    for d in date:
        d = str(d).split("-")[1] + "-" + str(d).split("-")[2]
        date_str.append(d)
    for i in range(0, len(data)):
        plt.plot(date_str, value[i], linewidth=3.0)
    plt.legend(name_cn)
    plt.ylabel('确诊人数(人)', fontdict={'size': 14})
    plt.xlabel('时间', fontdict={'size': 14})
    plt.xticks(date_str[0:-1:8], rotation=45)
    plt.show()


def dateRange(beginDate, endDate):
    dates = []
    dt = datetime.datetime.strptime(beginDate, "%Y-%m-%d")
    date = beginDate[:]
    while date <= endDate:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime("%Y-%m-%d")
    return dates


def dead_percent(name, name_cn):
    value = []
    data = []
    for i in range(0, len(name)):
        OneDayData = pd.read_excel('csv/OneDay/' + name[i] + '_OneDay.xlsx', usecol=['国家', '确诊人数', '死亡人数' '更新时间'])
        data.append(OneDayData)
        name.append(OneDayData.values[0][1])
    for i in range(0, len(data)):
        value_temp = []
        for j in range(0, data[i].values.shape[0]).__reversed__():
            confirm_number = data[i].values[j][2]
            dead_number = data[i].values[j][4]
            if confirm_number != 0:
                dead_per = float(dead_number) / float(confirm_number)
                value_temp.append(dead_per)
            else:
                value_temp.append(0)
        if len(value_temp) < data[4].values.shape[0]:
            temp = [0 for i in range(data[4].values.shape[0] - len(value_temp))]
            value_temp = temp + value_temp
        value.append(value_temp)

    # for i in range(0, len(value)):
    T = [i for i in range(0, len(value[0]))]
    for i in range(0, len(value)):
        plt.plot(T[::3], value[i][::3], linewidth=2.5)

    plt.ylabel('死亡率（%）', fontdict={'size': 14})
    plt.xlabel('天数', fontdict={'size': 14})
    plt.legend(name_cn)
    plt.show()


def now_confirm(name, name_cn, time):
    value = []
    data = []
    for i in range(0, len(name)):
        OneDayData = pd.read_excel('csv/OneDay/' + name[i] + '_OneDay.xlsx', usecol=['国家', '确诊人数', '死亡人数' '更新时间'])
        data.append(OneDayData)
        name.append(OneDayData.values[0][1])
    for i in range(0, len(data)):
        value_temp = []
        for j in range(0, data[i].values.shape[0]).__reversed__():
            confirm_number = data[i].values[j][2]
            cured_number = data[i].values[j][3]
            dead_number = data[i].values[j][4]
            now_number = confirm_number - cured_number - dead_number
            value_temp.append(now_number)

        print(len(value_temp))
        if len(value_temp) < len(time):
            temp = [0 for i in range(0, len(time))]
            value_temp = temp + value_temp
        else:
            value_temp = value_temp[0:100]
        value.append(value_temp)

    date_str = []
    for d in time:
        d = str(d).split("-")[1] + "-" + str(d).split("-")[2]
        date_str.append(d)
    for i in range(0, len(value)):
        plt.plot(date_str, value[i], linewidth=2.0)

    plt.ylabel('现存确诊人数(人）', fontdict={'size': 14})
    plt.xlabel('天数', fontdict={'size': 14})
    plt.xticks(date_str[0:-1:8],rotation=45)
    plt.legend(name_cn)
    plt.show()


if __name__ == '__main__':
    # confirm(name, dateRange('2020-02-24', '2020-06-02'), name_cn)
    # dead_percent(name[0:5], name_cn)
    now_confirm(name[0:5], name_cn[0:5], dateRange('2020-02-24', '2020-06-02'))
